(function(){
	LikeWidget = function (data){
		var This = this, elems = Sizzle(".widget,.count_label,.faces"), faceLis = Sizzle(".face");
		this.data = data;
		this.liked = data.liked;
		this.widget = $(elems[0]);
		this.countLabel = $(elems[1]);
		this.facesUL = $(elems[2]);
		if(faceLis.length>0&&data.uid > 0)
			this.selfFace = $(faceLis[0]);
		this.icon = $(Sizzle(".icon", this.widget)[0]);
		this.icon.setAttribute('title',this.liked? '取消喜欢':'');
		var button = $(Sizzle(".button")[0]);
		button.addEvent("click", function(e){
			if(This.data.uid==undefined || This.data.uid <= 0){
				if(This.loginW && !This.loginW.closed)
					This.loginW.focus();
				else This.loginW = window.open("http://www.connect.renren.com/like/login?url="+encodeURIComponent(This.data.url)+'&md5='+This.data.md5+'&post_form_id='+This.data.postFormId, "_blank", "resizable=yes,width=550,height=290,top=200,left="+(screen.availWidth-550)/2);
			} else {
				var s = e.target || e.srcElement;
				for (var t = s; t && t != this; t = t.parentNode) {
					if (t.className.search(/(\s+|^)like_label/) != -1 && !This.liked) {
						This.changeLike(true);
					} else if (t.className.search(/(\s+|^)icon/) != -1) {
						This.changeLike(!This.liked);
					}
				}
			}
		});
		if(XN.browser.IE6){
		    this.icon.hover('hover');
		    button.hover('hover');
		}
		var iframeW = document.documentElement.clientWidth||window.innerWidth, iframeH = document.documentElement.clientHeight||window.innerHeight;
		if(iframeW>130 && !this.widget.hasClassName('detail')){
			this.widget.addClass('detail');
		}
		this.maxFCount = Math.floor(iframeW/39)*Math.floor((iframeH-22)/39);
		if(data.showFaces===false||faceLis.length>=this.maxFCount||data.count<=(data.liked?1:0)){
			this.onFacesRequest([]);
		} else
			this.sendFacesRequest(this.maxFCount);
		
		this.data.qs = location.search.substring(1).replace(/&url=[^&]*|url=[^&]*&?/g, "");
	};
	
	$extend(LikeWidget.prototype, {
		openAdminWin : function(){
			if(this.adminW && !this.adminW.closed)
				this.adminW.focus();
			else this.adminW = window.open("http://www.connect.renren.com/like/admin?url="+encodeURIComponent(this.data.url), "_blank", "resizable=yes,width=550,height=290,top=200,left="+(screen.availWidth-550)/2);
		},
		changeLike : function(like){
			if(this.liked===like) return;
			if(like){
				this.widget.addClass('liked');
				if(this.selfFace && this.maxFCount>0) {
					if(this.lastFace) 
						this.lastFace.style.display='none';
					this.selfFace.show('fade');
				}
			} else {
				this.widget.delClass('liked');
				if(this.selfFace && this.maxFCount>0) {
					var This = this;
					XN.EFFECT.fadeOut(this.selfFace, function(e){
						e.style.display = 'none';
						if(This.lastFace) 
							This.lastFace.style.display='';
					});
				}
			}
			this.liked = like;
			this.sendLikeRequest();
			this.icon.setAttribute('title',this.liked? '取消喜欢':'');
		},
		sendLikeRequest : function(){
			var This = this;
			if(!this.requestRunning){
				this.requestRunning = true;
				new XN.net.xmlhttp({
					url : 'http://www.connect.renren.com/like/'+ (this.liked? 'like' : 'unlike'),
					data :'url='+encodeURIComponent(this.data.url)+'&md5='+this.data.md5+'&post_form_id='+this.data.postFormId+(this.data.qs&&this.liked?'&'+this.data.qs:''),
					onComplete : function(){This.requestRunning = false;}
		         });
			}
		},
		sendFacesRequest : function(fct){
			var This = this;
			new XN.net.xmlhttp({
				url : 'http://www.connect.renren.com/like/faces',
				data : 'url='+encodeURIComponent(this.data.url)+'&md5='+this.data.md5+'&count='+fct+'&post_form_id='+this.data.postFormId,
				onSuccess : function(r) {
					r = r.responseText;
					This.onFacesRequest(isJSON(r)? XN.JSON.parse(r): {result:-1});
			    }
	        });
		},
		onFacesRequest : function(json){
			if(json.result==-1) 
				json = [];
			var This = this, sFc = 0;
			XN.array.each(Sizzle(".face"), function(i, f){
				if(json.length>0&&f!==This.selfFace){
					This.facesUL.removeChild(f);
					return true;
				}
				if(XN.browser.IE6)
					$(f).hover('hover');
				if(This.maxFCount>sFc && (f!==This.selfFace||This.liked)) {
					f.style.display = '';
					sFc+=1;
				} else
					f.style.display = 'none';
				if(json.length==0&&This.selfFace&&This.maxFCount==sFc&&!This.lastFace&&!This.liked){
					This.lastFace = f;
				}
			});
			XN.array.each(json, function(i, f){
				if(This.selfFace && This.data.uid == f.id)
					return true;
				fli = $element("li");
				fli.addClass("face");
				var htmpl = '<img alt="" src="'+f.image+'" height="30px" width="30px"/>';
				if(f.name&&f.id)
					htmpl = '<a title="查看'+f.name+'的个人主页" href="http://www.renren.com/profile.do?id='+f.id+'" target="_blank">'+htmpl+'</a><span class="name">'+f.name+'</span>';
				fli.innerHTML = htmpl;
				if(This.maxFCount>sFc) {
					fli.style.display = '';
					sFc+=1;
				} else
					fli.style.display = 'none';
				This.facesUL.appendChild(fli);
				if(This.selfFace&&This.maxFCount==sFc&&!This.lastFace&&!This.liked){
					This.lastFace = fli;
				}
				if(XN.browser.IE6)
					$(fli).hover('hover');
			});
		}
	});
})();